//
//  GZAEDListController.swift
//  GZMarathon
//
//  Created by apple on 2021/10/29.
//

import UIKit
import MJRefresh
import SVProgressHUD
class GZAEDListController: EBBaseViewController {


    @IBOutlet weak var searchBar: SSSearchBar!
    
    @IBOutlet weak var tableView: UITableView!
    
    var dataSource: [GZAEDList] = []
    
    var curPage: Int = 0
        
    override func viewDidLoad() {
        super.viewDidLoad()
        confTableView()
        searchBar.delegate = self
        navigationController?.navigationBar.isHidden = true
    }
//
//    override func viewWillDisappear(_ animated: Bool) {
//        super.viewWillDisappear(animated)
//        navigationController?.navigationBar.isHidden = false
//    }
//
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getAEDList()
//        navigationController?.navigationBar.isHidden = true
    }

    
    func confTableView() {
        tableView.register(UINib(nibName: "GZAEDListCell", bundle: nil), forCellReuseIdentifier: "aed_list")
        tableView.rowHeight = 64
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.curPage = 0
            self?.getAEDList()
        })
//        tableView.mj_footer = MJRefreshAutoFooter(refreshingBlock: {[weak self] in
//            self?.curPage += 1
//            self?.getAEDList()
//        })
        tableView.mj_header?.beginRefreshing()
    }
    
    func endRefresh(){
        tableView.mj_header?.endRefreshing()
        tableView.mj_footer?.endRefreshing()
    }

    func getAEDList(){
        //0:防疫告警  1:健康告警
        ApiProvider.request(EBAPI.getAEDList(name: searchBar.text, seriNo: nil), model: GZAEDData.self) {[weak self] data, isSuccess in
            self?.endRefresh()
            guard let dt = data else{
                return
            }
            
            if self?.curPage == 0{
                self?.dataSource.removeAll()
            }
            self?.dataSource.append(contentsOf: dt.data)
            self?.tableView.reloadData()
        }        
    }
    
    
    @IBAction func clickLocationBtn(_ sender: UIButton) {
        guard let vc = UIStoryboard(name: "GM", bundle: nil).instantiateViewController(withIdentifier: "MapViewController") as? MapViewController else {
            return
        }
        vc.mapType = 1
        
        vc.AEDList = self.dataSource
   
        navigationController?.pushViewController(vc, animated: true)
    }
    
  
}


extension GZAEDListController: UITableViewDelegate, UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "aed_list", for: indexPath) as! GZAEDListCell
        let list = dataSource[indexPath.row]
        cell.setAEDList(list)
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
       
        guard let vc = UIStoryboard(name: "GM", bundle: nil).instantiateViewController(withIdentifier: "AEDSubViewController") as? AEDSubViewController else {
            return
        }
        
        vc.list = dataSource[indexPath.row]
        vc.type = dataSource[indexPath.row].aed_status.stringToInt
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
    
}



extension GZAEDListController: UISearchBarDelegate{
    
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        self.getAEDList()
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        self.getAEDList()
    }
    
}
